package com.example.LightningController;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: master
 * Date: 6/6/13
 * Time: 10:26 PM
 * To change this template use File | Settings | File Templates.
 */
public class DatabaseManager extends SQLiteOpenHelper {
    private SQLiteDatabase db;
    private static final String DB_NAME="Rooms";
    private static final String STORED_ROOMS_TABLE="StoredRooms";
    private static final String STORED_ROOMS_ID = "stored_rooms_id";
    private static final String STORED_ROOMS_NAME = "stored_rooms_name";
    private static final String STORED_ROOMS_MAC_ADDRESS = "stored_rooms_mac_address";
    private static final String STORED_ROOMS_DESCRIPTION = "stored_rooms_description";


    public DatabaseManager(Context context){
        super(context, DB_NAME, null, 8);

        db=this.getWritableDatabase();
    }

    public long insertIntoDatabase(String name, String macAddress){ //returns ID of newly inserted row
        db=this.getWritableDatabase();

        if (!macAddressExistsFromBefore(macAddress)){
            ContentValues cv=new ContentValues();
            cv.put(STORED_ROOMS_NAME, name);
            cv.put(STORED_ROOMS_MAC_ADDRESS, macAddress);
            return db.insert(STORED_ROOMS_TABLE, STORED_ROOMS_NAME, cv);
        } else{
            return -1;
        }
    }

    private boolean macAddressExistsFromBefore(String macAddress){
        db=this.getWritableDatabase();

        Cursor c = db.rawQuery("SELECT "+STORED_ROOMS_MAC_ADDRESS+" as MAC_ADDRESS FROM "+STORED_ROOMS_TABLE+" WHERE "+STORED_ROOMS_MAC_ADDRESS+"=?", new String []{macAddress});
        Log.i("after_insertion", ""+c.getCount());
        if(c.getCount() == 0)
        {
            Log.i("after_insertion", "return false");
            return false;
        }else{
            Log.i("after_insertion", "return true");
            return true;
        }
    }

    public int updateRecordInDatabase(String id, final String name, final String macAddress, final String description){
                //To change body of implemented methods use File | Settings | File Templates.
                db=DatabaseManager.this.getWritableDatabase();

                ContentValues cv=new ContentValues();
                cv.put(STORED_ROOMS_NAME, name);
                cv.put(STORED_ROOMS_MAC_ADDRESS, macAddress);
                cv.put(STORED_ROOMS_DESCRIPTION, description);

        return db.update(STORED_ROOMS_TABLE, cv, STORED_ROOMS_ID+"=?",
                new String []{id});
    }

    public int removeRecordFromDatabase(String id){
        Log.i("remove_record_from_database", id);
        db=this.getWritableDatabase();

        return db.delete(STORED_ROOMS_TABLE, STORED_ROOMS_ID+"=?", new String [] {String.valueOf(id)});
    }

    public long GetRecordID(String recordName, String recordMac)
    {
        SQLiteDatabase db=this.getReadableDatabase();

        Cursor c=db.query(STORED_ROOMS_TABLE, new String[]{STORED_ROOMS_ID+" as _id",STORED_ROOMS_NAME, STORED_ROOMS_MAC_ADDRESS},
                STORED_ROOMS_NAME+"=?", new String[]{recordName}, null, null, null);
        Log.i("get_record_id", ""+c.getCount());
        //Cursor c=db.rawQuery("SELECT "+colDeptID+" as _id FROM "+deptTable+"
        //WHERE "+colDeptName+"=?", new String []{Dept});
        c.moveToFirst();
        Log.i("get_record_id", ""+c.getLong(c.getColumnIndex("_id")));
        return c.getLong(c.getColumnIndex("_id"));
    }

    public ArrayList<Item> retrieveZones()
    {
        ArrayList<Item> zones = new ArrayList<Item>();

        SQLiteDatabase db=this.getReadableDatabase();

        Cursor c=db.query(STORED_ROOMS_TABLE, new String[]{STORED_ROOMS_ID+" as ID",STORED_ROOMS_NAME +" as Name", STORED_ROOMS_MAC_ADDRESS +" as MAC_ADDRESS"},
                null, null, null, null, null);
        if (!(c.getCount() == 0)){
            c.moveToFirst();
            while(!c.isLast()){
                for (int i = 0; i < c.getColumnCount() - 1; i++){
                    Log.i("list_zones", ""+c.getString(i));
                }
               zones.add(new Zone(c.getInt(0), c.getString(1), c.getString(2)));
               c.moveToNext();
            }
            zones.add(new Zone(c.getInt(0), c.getString(1), c.getString(2)));
        }

        return zones;
    }

    public String retrieveZonesDescription(String id){
        SQLiteDatabase db=this.getReadableDatabase();

        Cursor c=db.query(STORED_ROOMS_TABLE, new String[]{STORED_ROOMS_ID, STORED_ROOMS_DESCRIPTION + " as description"},
                STORED_ROOMS_ID+"=?", new String[]{id}, null, null, null);
        c.moveToFirst();

           for(int i = 0; i < c.getColumnCount() - 1; i++)
             Log.i("updating_record", c.getString(i));
        return c.getString(c.getColumnIndex("description"));
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i("oncreate_database", "onCreate database");
        db.execSQL("CREATE TABLE "+STORED_ROOMS_TABLE+" ("+STORED_ROOMS_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "+
                STORED_ROOMS_NAME+ " TEXT, " + STORED_ROOMS_MAC_ADDRESS+ " TEXT, " + STORED_ROOMS_DESCRIPTION+ " TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i2) {
        Log.i("oncreate_database", "onUpgrade database");
        db.execSQL("DROP TABLE IF EXISTS "+STORED_ROOMS_TABLE);
        onCreate(db);
    }
}
